<?php

namespace backend\models\download;

use Yii;

/**
 * This is the model class for table "edu_soft_course".
 *
 * @property integer $soft_id
 * @property integer $course_id
 */
class SoftCourse extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'edu_soft_course';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['soft_id', 'course_id'], 'required'],
            [['soft_id', 'course_id'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'soft_id' => 'Soft ID',
            'course_id' => 'Course ID',
        ];
    }
    
    /**
     * 软件相关课程 数据入库
     */
    public function Courseadd($data = array()) {
        $connection = Yii::$app->db; //连接
        $sql = " INSERT INTO `edu_soft_course` (`soft_id`, `course_id`) VALUES ";         
        foreach ($data as $key => $val) {
            $sql.="(".$val['soft_id'].",".$val['course_id']."),";
        }
        $sql = trim($sql,',');
        $command=$connection->createCommand($sql);        
        return $command->execute();
    }
    
    /**
     *软件编辑 软件修改入库
     */
    public function Updsoft() {
        $id = Yii::$app -> request->get('id');
        $this->deleteAll(['soft_id' => $id]);          
        foreach (Yii::$app->request->post('course_id') as $key => $value) {
            $newdata[] = array('soft_id'=>$id,'course_id'=>$value);
        }        
        $connection = Yii::$app->db; //连接
        $sql = " INSERT INTO `edu_soft_course` (`soft_id`, `course_id`) VALUES ";
        foreach ($newdata as $key => $val) {
            $sql.="(".$val['soft_id'].",".$val['course_id']."),";
        }
        $sql = trim($sql,',');
        $command=$connection->createCommand($sql);
        return $command->execute();
    }

    
}
